package com.business.web.spring;


import java.math.BigDecimal;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import com.business.domain.T_contract;
import com.business.domain.T_news;
import com.business.service.T_contractService;
import com.business.service.T_newsService;
import com.common.utils.DateUtil;
import org.apache.log4j.Logger;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.business.domain.T_task_node_instance;
import com.easyworkflow.web.spring.WorkFlowController;

//import com.workflow.WorkFlowUtils;
import com.common.db.JsonVO;
import com.common.db.Pg;
import com.common.utils.BeanUtils;
import com.business.domain.T_contract_bill;
//import com.business.mapper.T_contract_billMapper;
import com.business.service.T_contract_billService;

@Controller
@RequestMapping("/t_contract_bill")

public class T_contract_billController extends WorkFlowController {

    private static Logger log = Logger.getLogger(T_contract_billController.class.getName());

    @Resource
    private T_contract_billService t_contract_billService;
    @Resource
    private T_contractService t_contractService;
    @Resource
    private T_newsService t_newsService;


    //@Autowired
    //private WorkFlowUtils workFlowUtils;

    //@Resource
    //private T_contract_billMapper mapper;
    //private int id;
    //private int[] ids;
    private Pg pg;

    /**
     * 1、获取 T_contract_bill信息列表，并且在列表页面展示
     *
     * @param request
     * @return Pg，json格式
     */
    @RequestMapping("/getAllT_contract_bill")
    @ResponseBody
    public Pg getAllT_contract_bill(HttpServletRequest request) {
        int pageSize = 10;
        int startIndex = 0;
        T_contract_bill t_contract_bill = new T_contract_bill();
        try {
            if (request.getParameter("pageSize") != null) {
                pageSize = BeanUtils.stringToInt("pageSizeFlag", request.getParameter("pageSize"));
            }
            if (request.getParameter("startIndex") != null) {
                startIndex = BeanUtils.stringToInt("startIndexFlag", request.getParameter("startIndex"));
            }
            if (request.getParameter("process_code") != null) { //取得动作流 流程英文名称，后台查询数据关联使用
                t_contract_bill.setProcess_code(request.getParameter("process_code"));
            }
            if (request.getParameter("billno") != null && !"".equals(request.getParameter("billno"))) {
                t_contract_bill.setBillno(request.getParameter("billno"));
            }
            if (request.getParameter("contractno") != null && !"".equals(request.getParameter("contractno"))) {
                t_contract_bill.setContractno(request.getParameter("contractno"));
            }
            if (request.getParameter("buildingno") != null && !"".equals(request.getParameter("buildingno"))) {
                t_contract_bill.setBuildingno(request.getParameter("buildingno"));
            }
            if (request.getParameter("conpanyno") != null && !"".equals(request.getParameter("conpanyno"))) {
                t_contract_bill.setConpanyno(request.getParameter("conpanyno"));
            }
            if (request.getParameter("payday") != null && !"".equals(request.getParameter("payday"))) {
                t_contract_bill.setPayday(request.getParameter("payday"));
            }
            if (request.getParameter("info") != null && !"".equals(request.getParameter("info"))) {
                t_contract_bill.setInfo(request.getParameter("info"));
            }
            if (request.getParameter("billtime") != null && !"".equals(request.getParameter("billtime"))) {
                t_contract_bill.setBilltime(request.getParameter("billtime"));
            }
            if (request.getParameter("status") != null && !"".equals(request.getParameter("status"))) {
                t_contract_bill.setStatus(request.getParameter("status"));
            }
            if (request.getParameter("endday") != null && !"".equals(request.getParameter("endday"))) {
                t_contract_bill.setEndday(request.getParameter("endday"));
            }
            if (request.getParameter("billstartdate") != null && !"".equals(request.getParameter("billstartdate"))) {
                t_contract_bill.setBillstartdate(request.getParameter("billstartdate"));
            }
            if (request.getParameter("billenddate") != null && !"".equals(request.getParameter("billenddate"))) {
                t_contract_bill.setBillenddate(request.getParameter("billenddate"));
            }
            if (request.getParameter("area") != null && !"".equals(request.getParameter("area"))) {
                t_contract_bill.setArea(request.getParameter("area"));
            }
            if (request.getParameter("linktel") != null && !"".equals(request.getParameter("linktel"))) {
                t_contract_bill.setLinktel(request.getParameter("linktel"));
            }
            if (request.getParameter("billtype") != null && !"".equals(request.getParameter("billtype"))) {
                t_contract_bill.setBilltype(request.getParameter("billtype"));
            }
            if (request.getParameter("creater") != null && !"".equals(request.getParameter("creater"))) {
                t_contract_bill.setCreater(request.getParameter("creater"));
            }
            if (request.getParameter("creattime") != null && !"".equals(request.getParameter("creattime"))) {
                t_contract_bill.setCreattime(request.getParameter("creattime"));
            }
            if (request.getParameter("updater") != null && !"".equals(request.getParameter("updater"))) {
                t_contract_bill.setUpdater(request.getParameter("updater"));
            }
            if (request.getParameter("updatetime") != null && !"".equals(request.getParameter("updatetime"))) {
                t_contract_bill.setUpdatetime(request.getParameter("updatetime"));
            }
            if (request.getParameter("ext1") != null && !"".equals(request.getParameter("ext1"))) {
                t_contract_bill.setExt1(request.getParameter("ext1"));
            }
            if (request.getParameter("ext2") != null && !"".equals(request.getParameter("ext2"))) {
                t_contract_bill.setExt2(request.getParameter("ext2"));
            }
            if (request.getParameter("ext3") != null && !"".equals(request.getParameter("ext3"))) {
                t_contract_bill.setExt3(request.getParameter("ext3"));
            }
            if (request.getParameter("ext4") != null && !"".equals(request.getParameter("ext4"))) {
                t_contract_bill.setExt4(request.getParameter("ext4"));
            }
            if (request.getParameter("ext5") != null && !"".equals(request.getParameter("ext5"))) {
                t_contract_bill.setExt5(request.getParameter("ext5"));
            }
            if (request.getParameter("ext6") != null && !"".equals(request.getParameter("ext6"))) {
                t_contract_bill.setExt6(request.getParameter("ext6"));
            }
            if (request.getParameter("invoicestatus") != null && !"".equals(request.getParameter("invoicestatus"))) {
                t_contract_bill.setInvoicestatus(request.getParameter("invoicestatus"));
            }
            if (request.getParameter("invoiceno") != null && !"".equals(request.getParameter("invoiceno"))) {
                t_contract_bill.setInvoiceno(request.getParameter("invoiceno"));
            }
            if (request.getParameter("invoicedoner") != null && !"".equals(request.getParameter("invoicedoner"))) {
                t_contract_bill.setInvoicedoner(request.getParameter("invoicedoner"));
            }
            if (request.getParameter("invoicetime") != null && !"".equals(request.getParameter("invoicetime"))) {
                t_contract_bill.setInvoicetime(request.getParameter("invoicetime"));
            }
            if (request.getParameter("receiveinvoice") != null && !"".equals(request.getParameter("receiveinvoice"))) {
                t_contract_bill.setReceiveinvoice(request.getParameter("receiveinvoice"));
            }
            if (request.getParameter("sftype") != null && !"".equals(request.getParameter("sftype"))) {
                t_contract_bill.setSftype(request.getParameter("sftype"));
            }
            if (request.getParameter("paypredaytype") != null && !"".equals(request.getParameter("paypredaytype"))) {
                t_contract_bill.setPaypredaytype(request.getParameter("paypredaytype"));
            }
            if (request.getParameter("overduestatus") != null && !"".equals(request.getParameter("overduestatus"))) {
                t_contract_bill.setOverduestatus(request.getParameter("overduestatus"));
            }
            if (request.getParameter("invoicetype") != null && !"".equals(request.getParameter("invoicetype"))) {
                t_contract_bill.setInvoicetype(request.getParameter("invoicetype"));
            }
            if (request.getParameter("biztype") != null && !"".equals(request.getParameter("biztype"))) {
                t_contract_bill.setBiztype(request.getParameter("biztype"));
            }
            if (request.getParameter("billid") != null && !"".equals(request.getParameter("billid"))) {
                t_contract_bill.setBillid(new Integer(request.getParameter("billid")));
            }

            if (request.getParameter("startdate") != null && !"".equals(request.getParameter("startdate"))) {
                t_contract_bill.setStartdate(request.getParameter("startdate"));
            }
            if (request.getParameter("enddate") != null && !"".equals(request.getParameter("enddate"))) {
                t_contract_bill.setEnddate(request.getParameter("enddate"));
            }
            t_contract_bill.setStartNo(startIndex);
            t_contract_bill.setPageSize(pageSize);
            t_contract_bill.setEndNo(pageSize);
            List<T_contract_bill> findAll = t_contract_billService.findAll(t_contract_bill);
            int totalCount = t_contract_billService.findCountAll(t_contract_bill);
            pg = new Pg(findAll, totalCount, pageSize, startIndex);
            request.setAttribute("t_contract_billList", findAll);
            request.setAttribute("pg", pg);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("------query t_contract_billList error:" + e);
        }

        return pg;
    }

    /**
     * 2、新增保存  T_contract_bill信息并且返回成功或者失败信息json格式结果如：result:[success/error]
     *
     * @param t_contract_bill
     * @param request
     * @return
     */
    @RequestMapping("/saveT_contract_bill")
    @ResponseBody
    public JsonVO saveT_contract_bill(T_contract_bill t_contract_bill, HttpServletRequest request, HttpServletResponse response) {
        JsonVO vo = new JsonVO();
        vo.setResult("error");
        vo.setJieguo(false);
        t_contract_bill.setBillno(DateUtil.getCurrDateTime() + new Double(Math.random()).toString().substring(15));
        t_contract_bill.setCreattime(DateUtil.getCurrDateTime());
        t_contract_bill.setCreater(request.getSession().getAttribute("FREE_CURRENT_USER").toString());

        //如果有特殊需求，需要将json对象值转成java实体对象属性值
        if (t_contract_billService.save(t_contract_bill) > 0) {
            vo.setResult("success");
            vo.setJieguo(true);
        }
        return vo;
    }

    /**
     * 3。1、修改T_contract_bill信息并且返回成功或者失败信息json格式结果如：result:[success/error]
     *
     * @param t_contract_bill
     * @param request
     * @return JsonVO
     */
    @RequestMapping("/updateT_contract_bill")
    @ResponseBody
    public JsonVO updateT_contract_bill(T_contract_bill t_contract_bill, HttpServletRequest request) {
        JsonVO vo = new JsonVO();
        vo.setResult("error");
        vo.setJieguo(false);
        //如果有特殊值需要处理，在此处手工转换
        //T_contract_bill t_contract_billTemp = t_contract_billService.findById(t_contract_bill);
        //t_contract_billTemp.setXXX(t_contract_bill.getXXX());
        //if(t_contract_billService.update(t_contract_billTemp)){
        T_contract_bill temp = t_contract_billService.findById(t_contract_bill);
        temp.setUpdatetime(DateUtil.getCurrDateTime());
        temp.setUpdater(request.getSession().getAttribute("FREE_CURRENT_USER").toString());
        temp.setExt1(t_contract_bill.getExt1());
        temp.setBilltype(t_contract_bill.getBilltype());
        temp.setExt2(t_contract_bill.getExt2());
        temp.setBillstartdate(t_contract_bill.getBillstartdate());
        temp.setBillenddate(t_contract_bill.getBillenddate());

        temp.setExt3(t_contract_bill.getExt3());
        temp.setTotalprice(t_contract_bill.getTotalprice());
        temp.setExt4(t_contract_bill.getExt4());
        temp.setPayday(t_contract_bill.getPayday());
        temp.setStatus(t_contract_bill.getStatus());

        temp.setEndday(t_contract_bill.getEndday());
        temp.setExt5(t_contract_bill.getExt5());
        temp.setExt6(t_contract_bill.getExt6());
        temp.setContractno(t_contract_bill.getContractno());
        temp.setMinprice(t_contract_bill.getMinprice());

        if (t_contract_billService.update(temp) > 0) {
            vo.setResult("success");
            vo.setJieguo(true);
        }
        return vo;
    }

    /**
     * 3。2、提交审核业务数据
     * 修改T_contract_bill信息并且返回成功或者失败信息json格式结果如：result:[success/error]
     *
     * @param t_contract_bill
     * @param request
     * @return JsonVO
     */
    @RequestMapping("/tjshT_contract_bill")
    @ResponseBody
    public JsonVO tjshT_company(T_contract_bill t_contract_bill, HttpServletRequest request) {
        //启动工作流； 请假流程；
        return startMyTask(getTaskIdByBiztype(t_contract_bill.getBiztype()), t_contract_bill.getBiztype(), t_contract_bill.getBillid(), request);
    }

    /**
     * 3.3、提交T_contract_bill审核通过，并且返回成功或者失败信息json格式结果如：result:[success/error]
     *
     * @param t_task_node_instance
     * @param request
     * @return JsonVO
     */
    @RequestMapping("/shtgT_contract_bill")
    @ResponseBody
    public JsonVO shtgT_contract_bill(T_task_node_instance t_task_node_instance, HttpServletRequest request) {
        return doMyAgree(t_task_node_instance.getTninstanceid(), t_task_node_instance.getNote(), request);
    }

    /**
     * 3.4、提交T_contract_bill审核退回，信息并且返回成功或者失败信息json格式结果如：result:[success/error]
     *
     * @param t_task_node_instance
     * @param request
     * @return JsonVO
     */
    @RequestMapping("/shthT_contract_bill")
    @ResponseBody
    public JsonVO shthT_contract_bill(T_task_node_instance t_task_node_instance, HttpServletRequest request) {
        return doMyBack(t_task_node_instance.getTninstanceid(), t_task_node_instance.getNote(), request);
    }

    /**
     * 4、根据id查询T_contract_bill单个信息并显示在修改页面
     *
     * @param id
     * @param request
     * @return T_contract_bill json格式字符串
     */
    @RequestMapping("/getT_contract_bill")
    @ResponseBody
    public T_contract_bill getT_contract_bill(Integer id, HttpServletRequest request) {
        T_contract_bill t_contract_bill = new T_contract_bill();
        t_contract_bill.setBillid(id);
        return t_contract_billService.findById(t_contract_bill);
    }

    /**
     * 5、删除T_contract_bill根据id
     *
     * @param id
     * @param request
     * @param response return JsonVO ：json格式字符串
     */
    @RequestMapping("/delT_contract_bill")
    @ResponseBody
    public JsonVO delT_contract_bill(Integer id, HttpServletRequest request, HttpServletResponse response) {
        JsonVO vo = new JsonVO();
        vo.setResult("error");
        vo.setJieguo(false);

        T_contract_bill t_contract_bill = new T_contract_bill();
        t_contract_bill.setBillid(id);

        if (t_contract_billService.delete(t_contract_bill) > 0) {
            vo.setResult("success");
            vo.setJieguo(true);
        }
        return vo;
    }

    /**
     * 5、2 生效 or作废 T_contract_bill根据id
     *
     * @param id
     * @param request
     * @param response return JsonVO ：json格式字符串
     */
    @RequestMapping("/effT_contract_bill")
    @ResponseBody
    public JsonVO effT_contract_bill(Integer id, String status, HttpServletRequest request, HttpServletResponse response) {
        JsonVO vo = new JsonVO();
        vo.setResult("error");
        vo.setJieguo(false);

        T_contract_bill t_contract_bill = new T_contract_bill();
        t_contract_bill.setBillid(id);
        t_contract_bill = t_contract_billService.findById(t_contract_bill);
        t_contract_bill.setStatus(status);

        if (t_contract_billService.update(t_contract_bill) > 0) {
            vo.setResult("success");
            vo.setJieguo(true);
        }
        return vo;
    }

    /**
     * 6、跳转到t_contract_bill/toT_contract_billList查询所有数据页面
     * return /WEB-INF/views/business/t_contract_bill/t_contract_billList.jsp
     */
    @RequestMapping(value = "/toT_contract_billList")
    public String toT_contract_billList() {
        log.debug("------跳转到/WEB-INF/views/business/t_contract_bill/t_contract_billList.jsp------");
        return "business/t_contract_bill/t_contract_billList";
    }

    @RequestMapping(value = "/toT_contract_billListZujin")
    public String toT_contract_billListZujin() {
        log.debug("------跳转到/WEB-INF/views/business/t_contract_bill/t_contract_billListZujin.jsp------");
        return "business/t_contract_bill/t_contract_billListZujin";
    }

    @RequestMapping(value = "/toT_contract_billListShuidian")
    public String toT_contract_billListShuidian() {
        log.debug("------跳转到/WEB-INF/views/business/t_contract_bill/t_contract_billListShuidian.jsp------");
        return "business/t_contract_bill/t_contract_billListShuidian";
    }

    @RequestMapping(value = "/toT_contract_billCurdShuidian")
    public String toT_contract_billCurdShuidian() {
        log.debug("------跳转到/WEB-INF/views/business/t_contract_bill/t_contract_billCurdShuidian.jsp------");
        return "business/t_contract_bill/t_contract_billCurdShuidian";
    }

    @RequestMapping(value = "/toT_contract_billListShoukuan")
    public String toT_contract_billListSelf() {
        log.debug("------跳转到/WEB-INF/views/business/t_contract_bill/t_contract_billListShoukuan.jsp------");
        return "business/t_contract_bill/t_contract_billListShoukuan";
    }

    @RequestMapping(value = "/toT_contract_billListWarning")
    public String toT_contract_billListWarning() {
        log.debug("------跳转到/WEB-INF/views/business/t_contract_bill/t_contract_billListWarning.jsp------");
        return "business/t_contract_bill/t_contract_billListWarning";
    }
    @RequestMapping(value = "/toT_contract_billListFukuan")
    public String toT_contract_billListFukuan() {
        log.debug("------跳转到/WEB-INF/views/business/t_contract_bill/t_contract_billListFukuan.jsp------");
        return "business/t_contract_bill/t_contract_billListFukuan";
    }
    @RequestMapping(value = "/toT_contract_billCurdFukuan")
    public String toT_contract_billCurdFukuan() {
        log.debug("------跳转到/WEB-INF/views/business/t_contract_bill/t_contract_billCurdFukuan.jsp------");
        return "business/t_contract_bill/t_contract_billCurdFukuan";
    }
    @RequestMapping(value = "/toT_contract_billCurdShouKuan")
    public String toT_contract_billCurdShouKuan() {
        log.debug("------跳转到/WEB-INF/views/business/t_contract_bill/t_contract_billCurdShoukuan.jsp------");
        return "business/t_contract_bill/t_contract_billCurdShoukuan";
    }


    /**
     * 跳转到t_contract_bill/toT_contract_billList查询所有数据页面
     * return /WEB-INF/views/business/t_contract_bill/t_contract_billList.jsp
     */
    @RequestMapping(value = "/toT_contract_billNenghao")
    public String toT_contract_billNenghao() {
        log.debug("------跳转到/WEB-INF/views/business/t_contract_bill/t_contract_billListNenghao.jsp------");
        return "business/t_contract_bill/t_contract_billListNenghao";
    }

    /**
     * 7、新增、修改、详情页面t_contract_bill/toT_contract_billCurd跳转
     * business/t_contract_bill/T_contract_billCurd.jsp
     */
    @RequestMapping(value = "/toT_contract_billCurd")
    public String toT_contract_billCurd() {
        log.debug("------跳转到/WEB-INF/views/business/t_contract_bill/t_contract_billCurd.jsp------");
        return "business/t_contract_bill/t_contract_billCurd";
    }


    @RequestMapping(value = "/toT_contract_billPrint")
    public String toT_contract_billPrint(HttpServletRequest request ) {
        String oplx = request.getParameter("oplx");
        if("zujin".equals(oplx)){
            log.debug("------跳转到/WEB-INF/views/business/t_contract_bill/t_contract_billPrintZujin.jsp------");
            return "business/t_contract_bill/t_contract_billPrintZujin";
        }else if("wuyefei".equals(oplx)){
            log.debug("------跳转到/WEB-INF/views/business/t_contract_bill/t_contract_billPrintWuyefei.jsp------");
            return "business/t_contract_bill/t_contract_billPrintWuyefei";
        }else if("shuifei".equals(oplx)){
            log.debug("------跳转到/WEB-INF/views/business/t_contract_bill/t_contract_billPrintShuifei.jsp------");
            return "business/t_contract_bill/t_contract_billPrintShuifei";
        }else if("dianfei".equals(oplx)){
            log.debug("------跳转到/WEB-INF/views/business/t_contract_bill/t_contract_billPrintDianfei.jsp------");
            return "business/t_contract_bill/t_contract_billPrintDianfei";
        }else{
            return "";
        }
    }

}









